package ao.com.bna.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.poi.hssf.record.formula.functions.Dmax;
import org.hibernate.Query;
import org.hibernate.Session;

import ao.com.bna.beans.Devedor;
import ao.com.bna.beans.DmfasParticipante;
import ao.com.bna.beans.DmfasSigma;
import ao.com.bna.beans.SigmaParticipante;
import ao.com.bna.beans.TipoRelatorio;
import ao.com.bna.configuracao.ConexaoHibernate;
import ao.com.bna.util.BeanComboBoxItem;
import ao.com.bna.util.Constante;

public class DmfasSigmaDao extends AbstractHibernateDao<DmfasSigma> {
	
	public DmfasSigmaDao() {
		super(DmfasSigma.class);
	}

	@Override
	protected Session getSession() {
		return ConexaoHibernate.getSessionfactory().getCurrentSession();
	}

	public List<BeanComboBoxItem> listaDevedores(int sistema) {
		Query query = (Query)this.getSession().getNamedQuery("DmfasSigma.findAll");
		List<DmfasSigma> listaDmfasSigma = query.list();
		List<String> itemsString = new ArrayList<String>();
		
		for (DmfasSigma dmfasSigma : listaDmfasSigma) {
			itemsString.add(new String(dmfasSigma.getDevedor().trim()));
		}
		
		//Create a HashSet which allows no duplicates
		Set listaSet = new HashSet(itemsString);

		//Assign the HashSet to a new ArrayList
		itemsString = new ArrayList(listaSet) ;
		   
		//Ensure correct order, since HashSet doesn't
		Collections.sort(itemsString);		

		List<BeanComboBoxItem> listaDevedores = new ArrayList<BeanComboBoxItem>();
		listaDevedores.add(new BeanComboBoxItem("*", "«Todos»"));
		for (String item : itemsString) {
			listaDevedores.add(new BeanComboBoxItem(item, item));
		}
		return listaDevedores;
	}

	/*
	public List<Devedor> listaDevedores(int sistema) {
		String devedoresDmfas = "" + "select distinct pa.name as devedor "
				+ "from loan_part lp, " + "     parts pa "
				+ "where lp.cd_partyp = '10' " + "and   pa.pa_id = lp.pa_id "
				+ "order by pa.name ";

		String devedoresSigma = ""
				+ "select distinct pa.id as id, pa.lo_name as devedor "
				+ "from issue@sigma_db_link_test issue, "
				+ "     participant@sigma_db_link_test pa "
				+ "where issue.issuer_id = pa.id  " + "order by pa.lo_name ";

		switch (sistema) {
		case Constante.SISTEMA_DMFAS:
			return this.getSession().createSQLQuery(devedoresDmfas).list();
		case Constante.SISTEMA_SIGMA:
			return this.getSession().createSQLQuery(devedoresSigma).list();
			// case Constantes.SISTEMA_GLOBAL: return
			// this.session.createSQLQuery(moedaGlobal).list();
		default:
			return new ArrayList<Devedor>();
		}
	}*/

	public List<BeanComboBoxItem> listaMoedas(int sistema) {
		Query query = (Query)this.getSession().getNamedQuery("DmfasSigma.findAll");
		List<DmfasSigma> listaDmfasSigma = query.list();
		List<String> itemsString = new ArrayList<String>();
		
		for (DmfasSigma dmfasSigma : listaDmfasSigma) {
			itemsString.add(new String(dmfasSigma.getMoeda().trim()));
		}
		
		//Create a HashSet which allows no duplicates
		Set listaSet = new HashSet(itemsString);

		//Assign the HashSet to a new ArrayList
		itemsString = new ArrayList(listaSet) ;
		   
		//Ensure correct order, since HashSet doesn't
		Collections.sort(itemsString);		

		List<BeanComboBoxItem> listaMoedas = new ArrayList<BeanComboBoxItem>();
		listaMoedas.add(new BeanComboBoxItem("*", "«Todas»"));
		
		for (String item : itemsString) {
			listaMoedas.add(new BeanComboBoxItem(item, item));
		}
		return listaMoedas;
	}
	/*
	 * public List<Moeda> listaMoedas2(int sistema) { List<Moeda> moedas = new
	 * ArrayList<Moeda>();
	 * 
	 * 
	 * Query query = (Query) this.session.getNamedQuery("LocPais.findByCode");
	 * query.setParameter("code", code); return query.list();
	 * 
	 * switch (sistema) { //case Constante.SISTEMA_DMFAS: moedas =
	 * this.getSession().createSQLQuery(MyQuery.MOEDA_DMFAS).list(); //case
	 * Constante.SISTEMA_DMFAS: moedas =
	 * this.getSession().getNamedQuery(MyQuery.MOEDA_DMFAS).addEntity("moeda",
	 * Moeda
	 * .class).setResultTransformer(Transformers.aliasToBean(Moeda.class)).list
	 * (); case Constante.SISTEMA_DMFAS: moedas =
	 * this.getSession().getNamedQuery
	 * ("DmfasSigma.findMoedas").setResultTransformer
	 * (Transformers.aliasToBean(Moeda.class)).list(); for (Moeda moeda :
	 * moedas) { JOptionPane.showMessageDialog(null, moeda.toString()); }
	 * //.setResultTransformer(Transformers.aliasToBean(Moeda.class)).list();
	 * //case Constante.SISTEMA_DMFAS: return
	 * this.getSession().createSQLQuery(moedaDmfas
	 * ).addEntity(Moeda.class).list(); case Constante.SISTEMA_SIGMA: return
	 * this
	 * .getSession().createSQLQuery(MyQuery.MOEDA_SIGMA).addEntity(Moeda.class
	 * ).list(); case Constante.SISTEMA_GLOBAL: return
	 * this.getSession().createSQLQuery
	 * (MyQuery.MOEDA_GLOBAL).addEntity(Moeda.class).list(); default: return new
	 * ArrayList<Moeda>(); } }
	 */

	public List<TipoRelatorio> listarTipoRelatorio() {
		TipoRelatorioDao dao = new TipoRelatorioDao();
		List<TipoRelatorio> lista = dao.listarTipoRelatorio();
		return lista;
	}

	/*
	public List<SigmaParticipante> listaDevedoresSigma() {
		SigmaParticipanteDao dao = new SigmaParticipanteDao();		
		return dao.listar();
	}

	public List<DmfasParticipante> listaDevedoresDmfas() {
		DmfasParticipanteDao dao = new DmfasParticipanteDao();		
		return dao.listar();
	}*/
}
